const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const session = require('express-session');
const template = require('art-template');
const dateFormat = require('dateformat');
// const morgan = require('morgan');
const app = express();
require('./model/connect');
app.use(session({secret: 'secret key', saveUninitialized: false}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'art');
app.engine('art', require('express-art-template'));
template.defaults.imports.dateFormat = dateFormat;
// if(process.env.NODE_ENV == 'development') {
// 	console.log('开发环境');
// 	app.use(morgan('dev'));
// } else {
// 	console.log('生产环境')
// }
const home = require('./route/home');
const admin = require('./route/admin');
app.use('/admin', require('./middleware/loginGuard'));
app.use('/home', home);
app.use('/admin', admin);
app.use((err, req, res, next) => {
	const result = JSON.parse(err);
	let params = [];
	for(let k in result) {
		if(k != path) {
			params.push(k + '=' + result[k]);
		}
	}
	res.redirect(`${result.path}?${params.join('&')}`);
});
app.listen(80);
console.log('服务器ok');